<html>
<head><meta charset="utf-8"><title>What is a cgu or rcgu file? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/What.20is.20a.20cgu.20or.20rcgu.20file.3F.html">What is a cgu or rcgu file?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="205765415"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/What%20is%20a%20cgu%20or%20rcgu%20file%3F/near/205765415" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/What.20is.20a.20cgu.20or.20rcgu.20file.3F.html#205765415">(Aug 03 2020 at 07:16)</a>:</h4>
<p>When I unpack a .a file generated for a staticlib I get lots of rcgu files with the same name, things like <code>blah-cgu.15.rcgu.o</code>. What do "cgu" and "rcgu" here mean? I'm guessing cgu stands for "code gen unit" but I'm not sure.</p>



<a name="205768421"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/What%20is%20a%20cgu%20or%20rcgu%20file%3F/near/205768421" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/What.20is.20a.20cgu.20or.20rcgu.20file.3F.html#205768421">(Aug 03 2020 at 08:03)</a>:</h4>
<p>Yes, cgu is "code gen unit". rcgu is "rust code gen unit".</p>



<a name="205770785"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/What%20is%20a%20cgu%20or%20rcgu%20file%3F/near/205770785" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/What.20is.20a.20cgu.20or.20rcgu.20file.3F.html#205770785">(Aug 03 2020 at 08:33)</a>:</h4>
<p>Thanks. I thought a crate is a single compilation for rustc, is this not correct anymore? Why and how does rustc split a crate into multiple compilation units?</p>



<a name="205772521"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/What%20is%20a%20cgu%20or%20rcgu%20file%3F/near/205772521" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/What.20is.20a.20cgu.20or.20rcgu.20file.3F.html#205772521">(Aug 03 2020 at 08:57)</a>:</h4>
<p>A crate is split into multiple codegen units just before codegen so multiple codegen units can be optimized in parallel. You can the control amount using <code>-Ccodegen-units</code>.</p>



<a name="205773149"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/What%20is%20a%20cgu%20or%20rcgu%20file%3F/near/205773149" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/What.20is.20a.20cgu.20or.20rcgu.20file.3F.html#205773149">(Aug 03 2020 at 09:07)</a>:</h4>
<p>Thanks! By "codegen" do you mean when generating LLVM modules of even before that? (maybe when generating MIR?) I just started passing <code>-Ccodege-units</code> to core, std, compiler_builtins and the main crate and it's much easier to find symbols I'm looking for now.</p>



<a name="205773780"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/What%20is%20a%20cgu%20or%20rcgu%20file%3F/near/205773780" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/What.20is.20a.20cgu.20or.20rcgu.20file.3F.html#205773780">(Aug 03 2020 at 09:16)</a>:</h4>
<p>"codegen" is generating and optimizing LLVM modules.</p>



<a name="205774514"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/What%20is%20a%20cgu%20or%20rcgu%20file%3F/near/205774514" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/What.20is.20a.20cgu.20or.20rcgu.20file.3F.html#205774514">(Aug 03 2020 at 09:26)</a>:</h4>
<p>So MIR optimizations applied to the whole crate, right?</p>



<a name="205774817"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/What%20is%20a%20cgu%20or%20rcgu%20file%3F/near/205774817" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/What.20is.20a.20cgu.20or.20rcgu.20file.3F.html#205774817">(Aug 03 2020 at 09:30)</a>:</h4>
<p>MIR optimizations normally look at a single MIR body at a time. Only MIR inlining looks at multiple MIR bodies at a time, but even in that case it can only change a single MIR body at a time and it needs a hackish scheme to prevent cycle errors, as it looks at the already optimized MIR of callees.</p>



<a name="205774830"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/What%20is%20a%20cgu%20or%20rcgu%20file%3F/near/205774830" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/What.20is.20a.20cgu.20or.20rcgu.20file.3F.html#205774830">(Aug 03 2020 at 09:30)</a>:</h4>
<p>MIR optimizations are per-function, but this can require pulling info from the rest of the crate or even other crates</p>



<a name="205774964"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/What%20is%20a%20cgu%20or%20rcgu%20file%3F/near/205774964" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/What.20is.20a.20cgu.20or.20rcgu.20file.3F.html#205774964">(Aug 03 2020 at 09:32)</a>:</h4>
<p>since MIR opts can trigger e.g. const eval, which itself can require the MIR for some const fns, there is a dependency tree here, but as bjorn3 said, only in inlining do you get cycle errors that are problematic. All other cycle errors that you get are legitly cycles in your code</p>



<a name="205798854"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/What%20is%20a%20cgu%20or%20rcgu%20file%3F/near/205798854" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matthiaskrgr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/What.20is.20a.20cgu.20or.20rcgu.20file.3F.html#205798854">(Aug 03 2020 at 14:16)</a>:</h4>
<p>oh so there are no full-program mir opts?</p>



<a name="205803731"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/What%20is%20a%20cgu%20or%20rcgu%20file%3F/near/205803731" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/What.20is.20a.20cgu.20or.20rcgu.20file.3F.html#205803731">(Aug 03 2020 at 14:57)</a>:</h4>
<p>nope</p>



<a name="205803790"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/What%20is%20a%20cgu%20or%20rcgu%20file%3F/near/205803790" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/What.20is.20a.20cgu.20or.20rcgu.20file.3F.html#205803790">(Aug 03 2020 at 14:57)</a>:</h4>
<p>full-program mir opts don't go very well with the query system.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>